package cn.lingyangwl.agile.member.controller.admin;

import cn.lingyangwl.agile.member.model.rqrs.user.MemberUserPageReq;
import cn.lingyangwl.agile.member.model.rqrs.user.MemberUserResp;
import cn.lingyangwl.agile.member.model.rqrs.user.MemberUserSaveReq;
import cn.lingyangwl.agile.member.model.rqrs.user.MemberUserUpdateReq;
import cn.lingyangwl.agile.member.service.MemberUserService;
import cn.lingyangwl.agile.model.constants.ValidationCons;
import cn.lingyangwl.agile.model.enums.TerminalEnum;
import cn.lingyangwl.agile.ram.ActionType;
import cn.lingyangwl.agile.ram.RamResource;
import cn.lingyangwl.agile.system.model.rqrs.user.UserStatusUpdateReq;
import cn.lingyangwl.framework.core.response.Resp;
import cn.lingyangwl.framework.core.utils.IpUtils;
import cn.lingyangwl.framework.tool.core.exception.Assert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 会员用户
 * 
 * @author shenguangyang
 */
@Api(tags = "会员用户")
@RestController
@RequestMapping("/member/user")
public class MemberUserController {
    @Resource
    private MemberUserService memberUserService;

    @ApiOperation(value = "分页会员用户")
    @GetMapping("/page")
    @RamResource(action = "member:user:query", name = "查询会员用户", type = ActionType.READ)
    public Resp<IPage<MemberUserResp>> pageMemberUser(@Validated MemberUserPageReq req) {
        return Resp.ok(memberUserService.pageUser(req));
    }

    @ApiOperation(value = "通过id查找会员用户")
    @GetMapping("/get")
    @RamResource(action = "member:user:query", name = "查询会员用户", type = ActionType.READ)
    public Resp<MemberUserResp> getMemberUser(@RequestParam(value = "id") Long id) {
        return Resp.ok(memberUserService.getUser(id));
    }
  
    @ApiOperation(value = "保存会员用户")
    @PostMapping("/save")
    @RamResource(action = "member:user:save", name = "保存会员用户", type = ActionType.WRITE)
    public Resp<?> saveMemberUser(@Validated @RequestBody MemberUserSaveReq req) {
        req.setRegisterIp(IpUtils.getHostIp());
        req.setRegisterTerminal(TerminalEnum.ADMIN.getTerminal());
        return Resp.ok(memberUserService.saveUser(req));
    }
  
    @ApiOperation(value = "更新会员用户")
    @PostMapping("/update")
    @RamResource(action = "member:user:update", name = "更新会员用户", type = ActionType.WRITE)
    public Resp<?> updateMemberUser(@Validated @RequestBody MemberUserUpdateReq req) {
        Assert.notNull(req.getId(), "id" + ValidationCons.NOT_EMPTY);
        return Resp.ok(memberUserService.updateUser(req));
    }
  
    @ApiOperation(value = "批量删除会员用户")
    @DeleteMapping("/batchDelete")
    @RamResource(action = "member:user:delete", name = "删除会员用户", type = ActionType.WRITE)
    public Resp<?> batchDeleteMemberUser(@RequestParam(value = "ids") List<Long> ids) {
        return Resp.ok(memberUserService.batchDeleteUser(ids));
    }
  
    @ApiOperation(value = "删除会员用户")
    @DeleteMapping("/delete")
    @RamResource(action = "member:user:delete", name = "删除会员用户", type = ActionType.WRITE)
    public Resp<?> deleteMemberUser(@RequestParam(value = "id") Long id) {
        return Resp.ok(memberUserService.deleteUser(id));
    }

    /**
     * 冻结用户
     */
    @PutMapping("/frozenBatch")
    @RamResource(action = "member:user:frozenBatch", name = "冻结用户", type = ActionType.WRITE)
    public Resp<?> frozenBatch(@RequestBody @Validated UserStatusUpdateReq req) {
        memberUserService.updateBatchUserStatus(req);
        return Resp.ok();
    }

    @PutMapping("/adjustPoint")
    @RamResource(action = "member:user:adjustPoint", name = "调整用户积分", type = ActionType.WRITE)
    public Resp<?> adjustPoint(
            @RequestParam("id") Long userId,
            @RequestParam("updatePoint") Integer point,
            @RequestParam("remark") String remark) {
        memberUserService.adjustPoint(userId, point, remark);
        return Resp.ok();
    }

}
